<template>
  <el-dialog
    title="流量充值"
    v-model="state.dialogVisible"
    width="800"
    close="close"
  >
    <el-form
      ref="formRef"
      :model="query"
      label-width="100px"
      class="demo-ruleForm"
      @submit.native.prevent
      :inline="true"
    >
      <el-form-item label="充值类型" prop="type">
        <el-select
          v-model="query.type"
          placeholder="请选择类型"
          style="width: 200px"
          clearable
        >
          <el-option value="CZ" label="流量充值">流量充值</el-option>
          <el-option value="KJ" label="日终扣减">日终扣减</el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="handle">
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="state.loading"
      :data="state.tableData"
      border
      :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
      tooltip-effect="dark"
      :style="{ width: '100%' }"
      class="dataTable"
    >
      <el-table-column prop="serialNum" label="流水号" width="180">
      </el-table-column>
      <el-table-column prop="trafficUnitPrice" label="流量单价">
        <template #default="scope">{{
          scope.row.trafficUnitPrice
            ? scope.row.trafficUnitPrice + " 元/GB"
            : ""
        }}</template>
      </el-table-column>
      <el-table-column prop="bandwidth" label="充值/扣减流量">
        <template #default="scope">{{
          scope.row.bandwidth ? scope.row.bandwidth + " GB" : ""
        }}</template>
      </el-table-column>
      <el-table-column prop="money" label="充值/扣减金额">
        <template #default="scope">{{
          scope.row.money ? scope.row.money + "元" : ""
        }}</template>
      </el-table-column>
      <el-table-column prop="type" label="类型">
        <template #default="scope">{{
          scope.row.type == "KJ" ? "日终扣减" : "流量充值"
        }}</template>
      </el-table-column>
      <el-table-column prop="recordTime" label="记录时间"> </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page.page"
      :page-size="page.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      background
      :total="page.total"
      style="margin: 12px 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed, watch } from "vue";
import { postShopRecord } from "@/api/business";

const emits = defineEmits(["ok"]);

const state = reactive({
  dialogVisible: false,
  trafficUnitPrice: 0.1,
  shopId: "",
  loading: false,
  tableData: [],
  typeOptions: [
    { label: "流量充值", value: "CZ" },
    { label: "日终扣减", value: "KJ" },
  ],
});

const formRef = ref();
const query = reactive({
  type: "",
});
const page = reactive({
  page: 1,
  pageSize: 10,
  total: 0,
});
const rules = reactive({
  type: [{ required: true, message: "请输入充值金额", trigger: "blur" }],
});

const resGB = computed(() => {
  return Math.floor((form.price / state.trafficUnitPrice) * 100) / 100;
});

function close() {
  state.dialogVisible = false;
  formRef.value.resetFields();
  state.tableData = [];
}

function getTableData() {
  state.loading = true;
  postShopRecord({
    type: query.type,
    shopId: state.shopId,
    page: page.page,
    pageSize: page.pageSize,
  })
    .then((res) => {
      state.tableData = res.data.list;
      page.total = res.data.total;
    })
    .finally(() => {
      state.loading = false;
    });
}
function handleSearch() {
  getTableData();
}
function handleReset() {
  formRef.value.resetFields();
  page.page = 1;
  getTableData();
}
// 分页
const handleSizeChange = (val) => {
  page.pageSize = val;
  getTableData();
};
const handleCurrentChange = (val) => {
  page.page = val;
  getTableData();
};
function show(data) {
  state.dialogVisible = true;
  state.shopId = data.shopId;
  state.tableData = [];
  getTableData();
}

defineExpose({ show });
</script>
